from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC

iris = load_iris()
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)
pca = PCA(n_components=2)
x_train_pca = pca.fit_transform(x_train)  # 对训练数据执行PCA拟合和转换
x_test_pca = pca.transform(x_test)  # 使用相同的PCA参数对测试数据进行转换

model = SVC(kernel='linear')
model.fit(x_train_pca, y_train)
train_score = model.score(x_train_pca, y_train)
test_score = model.score(x_test_pca, y_test)
print("训练得分:", train_score)
print("预测得分:", test_score)
